package com.banciyuan.bcywebview.biz.detail.charge;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.android.volley.RequestQueue;
import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.base.helper.ActionbarHelper;
import com.banciyuan.bcywebview.base.helper.ProgressbarHelper;
import com.banciyuan.bcywebview.base.view.pulltorefresh.PullToRefreshBase;
import com.banciyuan.bcywebview.base.view.pulltorefresh.PullToRefreshListView;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.daoexample.model.ChargeItem;
import de.greenrobot.daoexample.model.UserDetail;

/**
 * Created by squall on 2016/5/17.
 */
public class ChargeRankActivity extends BaseActivity {

    private PullToRefreshListView refreshListView;
    private ListView mListView;
    private ChargeRankAdapter adapter;
    private ProgressbarHelper mProgressbarHelper;
    private View mProgressbarView;
    private RequestQueue queue;
    private List<UserDetail> ulist = new ArrayList<>();
    private ActionbarHelper mActionbarHelper;
    private View mActionBarView;
    private int page = 1;
    private boolean isEnd = false;

    private ChargeItem chargeItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_topic_list);
        initArgs();
        initProgressbar();
        initActionbar();
        initUi();
        initAction();
        initData();
    }

    @Override
    protected void initArgs() {
        queue = VolleyQueue.getRquest(this);
        chargeItem = (ChargeItem) getIntent().getSerializableExtra(gotoUtil.INTENT_VALUE_ONE);
    }

    @Override
    protected void initUi() {
        refreshListView = (PullToRefreshListView) findViewById(R.id.topic_refresh_lv);
        mListView = refreshListView.getRefreshableView();
    }

    @Override
    protected void initData() {
        PayHelper payHelper = new PayHelper(this);
        payHelper.getPayList(new PayHelperInter() {
            @Override
            public void Succ(String response) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String result = jsonObject.getString(HttpUtils.UPLOAD_DATA);
                    Gson gson = new Gson();
                    List<UserDetail> templist = gson.fromJson(result, new TypeToken<List<UserDetail>>() {
                    }.getType());
                    dealJson(templist);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void fail() {

            }
        }, chargeItem, page);
    }

    private void dealJson(List<UserDetail> temp) {
        if (page == 1) {
            ulist.clear();
        }
        if (temp.isEmpty()) {
            isEnd = true;
        }
        ulist.addAll(temp);
        if (null != adapter) {
            adapter.notifyDataSetChanged();
        } else {
            adapter = new ChargeRankAdapter(ulist, ChargeRankActivity.this);
            refreshListView.setAdapter(adapter);
        }
        mProgressbarHelper.onSuccess();
        refreshListView.onRefreshComplete();
    }

    @Override
    protected void initProgressbar() {
        mProgressbarView = findViewById(R.id.base_progressbar);
        mProgressbarHelper = new ProgressbarHelper(mProgressbarView);
        mProgressbarHelper.setActionCallbacks(new ProgressbarHelper.OnProgressCallbacks() {
            @Override
            public void onActionClick(int id) {
                mProgressbarHelper.onLoading();
                initData();
            }
        });
        mProgressbarHelper.onLoading();
    }

    @Override
    protected void initAction() {
        refreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                page = 1;
                isEnd = false;
                initData();
            }
        });

        refreshListView.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {
            @Override
            public void onLastItemVisible() {
                if (!isEnd) {
                    page++;
                    initData();
                }
            }
        });

    }

    @Override
    protected void initActionbar() {
        mActionBarView = findViewById(R.id.base_action_bar);
        mActionbarHelper = new ActionbarHelper(this, mActionBarView);
        mActionbarHelper.setTitle(getString(R.string.charge_rank));
    }
}
